import { ApiCall } from "tsrpc";
import { ReqStartFishing, ResStartFishing } from "../../../shared/master/word/PtlStartFishing";
import { player } from "../../../model/fight/body/player";
import { template } from "../../../shared/master/MsgAction";
import app, { xlsxCfgNames } from "../../../app";
import manage_user from "../../model/manage/manage_user";
import { rank_type, taskKey } from "../../../shared/interface";
import manage_task from "../../model/manage/manage_task";
import god from "../../../god";
let fishMap: Map<string, { time: number, close: boolean, fish: number }> = new Map();
let timer;
function callAll(msg: string) {
    fishMap.forEach((info, uuid: string) => {
        let user = manage_user.locaHas(uuid);
        if (!user) {
            return;
        }

    });
}
export default async function (call: ApiCall<ReqStartFishing, ResStartFishing>) {
    call.succ({})
    let user = call.req._player as player;
    if (call.req.end) {
        let info = fishMap.get(user.uuid);
        if (!info) {
            user.sendMsg(template.default, `🐟️你还没有开始钓鱼哦`)
            return;
        }
        info.close = true;
        fishMap.set(user.uuid, info);
        return;
    }
    fishMap.set(user.uuid, {
        time: 0,
        close: false,
        fish: 0
    });
    let one = await god.oneMore()
    user.sendMsg(template.default, `「${one.str}」\n🐟️钓鱼开始，当前钓友:${fishMap.size}人,请耐心等待上钩🪝\n🐟️钓鱼等级${user.leve_fishing.num}(${app.BN(user.leve_fishing.now)}/${app.BN(user.leve_fishing.max)})`)

    if (!timer) {
        timer = setInterval(() => {
            fishMap.forEach(async (info, uuid: string) => {
                let _user = manage_user.locaHas(uuid);
                if (!_user) {
                    fishMap.delete(uuid);
                    return;
                }
                if (_user.get_battle()) {
                    fishMap.delete(uuid);
                    return;
                }
                let cell = _user.getPosition();
                if (!cell || !cell.get_water()) {
                    fishMap.delete(uuid);
                    return;
                }
                if (info.time == 0) {
                    let one = await god.oneMore()
                    info.time = Date.now() + app.random(3, 10) * 1000;
                    _user.sendMsg(template.default_none, `「${one.str}」\n🐟️开始投放鱼饵和饲料`)
                    return;
                }
                if (info.time > Date.now()) {
                    if (info.close) {
                        _user.sendMsg(template.default_none, `🐟️提前收杆鱼已经跑掉了😭`)
                        fishMap.delete(uuid)
                        return;
                    }
                    let one = await god.oneMore()
                    _user.sendMsg(template.default_none, `「${one.str}」\n🐟️钓鱼中...请耐心等待`)
                    return;
                }
                if (info.fish && !info.close && Date.now() - info.time > 14000) {
                    _user.sendMsg(template.default_none, `😭没有及时[收杆]鱼已经跑掉了`)
                    fishMap.delete(uuid)
                    return;
                }
                if (info.fish && info.close) {
                    const cfg_fishMap = app.getCfg(xlsxCfgNames.鱼库);
                    let info = cfg_fishMap.get(app.random(1, cfg_fishMap.size).toString())
                    let jin = app.random(info.min, info.max)
                    let cont = app.random(_user.leve_fishing.num, _user.leve_fishing.num * 2)
                    let item = app.createCfgItem('prop-21', 0, `钓友·${_user.name}`)
                    let exp = 1;
                    if (app.random(0, 100) < 10) {
                        cont = jin + _user.leve_fishing.num;
                        item.set_cont(cont)
                        exp = app.random(1, 10)
                        _user.sendMsg(template.default_none, `🎉钓鱼大成功！\n${info.name}(${jin}斤)\n${info.desc}\n🔎你获得🐟️小鱼干x${cont}\n🐟️钓鱼经验+${exp}`)
                        _user.addExp_fishing(exp)
                    } else {
                        item.set_cont(cont)
                        _user.sendMsg(template.default_none, `🎉钓鱼成功！\n${info.name}(${jin}斤)\n${info.desc}\n🔎你获得🐟️小鱼干x${cont}\n🐟️钓鱼经验+${exp}`)
                        _user.addExp_fishing(exp)
                    }
                    manage_task.event(taskKey.钓到鱼只, 1, _user)
                    manage_task.event(taskKey.钓到鱼斤, jin, _user)
                    _user.log.add(rank_type.钓鱼总斤数, jin)
                    _user.getBag().add(item);
                    fishMap.delete(uuid);
                    let one = await god.oneMore()
                    callAll(`「${one.str}」\n🐟️钓友:${_user.nickName} 钓到了${jin}斤的${info.name}!`)
                    return;
                }
                if (app.random(1, 100) > 10) {
                    info.fish = app.random(1, 10);
                    _user.sendMsg(template.default_none, `🔔🔔鱼已上钩!请在5秒内发送🔔[收杆]🔔🔔`)
                }
            })
        }, 3000)
    }
}